package cn.hdy.base.service.impl;

import cn.hdy.base.mapper.BaseMapper;
import cn.hdy.base.query.BaseQuery;
import cn.hdy.base.service.IBaseService;
import cn.hdy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//公共的实现类方法
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    public List<T> findAll() {
        return  baseMapper.findAll();
    }



    @Override
    public PageList<T> queryPage(BaseQuery query) {
        PageList<T> pageList = new PageList<>();
        Long count = baseMapper.queryCount(query);
        if (count == null || count == 0) {
            return pageList;
        }
        List<T> tList = baseMapper.queryData(query);
        pageList.setData(tList);
        pageList.setTotals(count);
        return pageList;
    }
}
